import 'dart:convert';

import 'package:bmprogresshud/bmprogresshud.dart';
import 'package:car_baidu/core/service/http_config.dart';
import 'package:car_baidu/core/service/login_request.dart';
import 'package:car_baidu/ui/page/main/main.dart';
import 'package:car_baidu/ui/shared/theme/app_theme.dart';
import 'package:car_baidu/ui/widget/single_input.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import '../../shared/px/double_extentsion.dart';
import '../../shared/px/int_extentsion.dart';
import '../../widget/center_widget.dart';

class LLLoginContent extends StatefulWidget {
  const LLLoginContent({Key? key}) : super(key: key);

  @override
  _LLLoginContentState createState() => _LLLoginContentState();
}

class _LLLoginContentState extends State<LLLoginContent> {
  final TextEditingController _phoneController = TextEditingController();

  final TextEditingController _passwordController = TextEditingController();

  final FocusNode _phoneFocusNode = FocusNode();
  final FocusNode _passwordFocusNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: SafeArea(
        child: Container(
          width: double.infinity,
          padding: EdgeInsets.only(top: 102.px, left: 20.px, right: 20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 头部
              getHeaderWidget(context),

              getCenterWidget(context),
              getBottomWiget(context),
            ],
          ),
        ),
      ),
    );
  }

  Widget getHeaderWidget(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Image.asset(
          "asset/image/login_topImage.png",
          width: 76.5.px,
        ),
        SizedBox(
          height: 3.px,
        ),
        Row(children: [
          SizedBox(width: 6.5.px),
          Text(
            "欢迎来到路叮叮",
            style: Theme.of(context)
                .textTheme
                .headline3!
                .copyWith(color: Color.fromARGB(255, 51, 51, 51)),
          )
        ]),
      ],
    );
  }

  Widget getCenterWidget(BuildContext context) {
    return Column(
      children: [
        LLCenterWidget(
          horizontal: 8.5.px,
          height: 57.5.px,
          child: LLSingleInputWidget(
            controller: _phoneController,
            leading: Image.asset(
              "asset/image/login_phone.png",
              width: 15.px,
            ),
            placeHold: "请输入手机号",
            focusNode: _phoneFocusNode,
          ),
        ),
        LLCenterWidget(
          horizontal: 8.5.px,
          height: 57.5.px,
          child: LLSingleInputWidget(
            controller: _passwordController,
            leading: Image.asset(
              "asset/image/login_password.png",
              width: 15.px,
            ),
            placeHold: "请输入密码",
            focusNode: _passwordFocusNode,
          ),
        ),
      ],
    );
  }

  Widget getBottomWiget(BuildContext context) {
    TextStyle textStyle =
        TextStyle(color: Color.fromARGB(255, 153, 153, 153), fontSize: 11);
    return Column(
      children: [
        LLCenterWidget(
          decoration: BoxDecoration(
              color: LLAppTheme.primaryColor,
              borderRadius: BorderRadius.circular(3)),
          height: 42.px,
          horizontal: 19.px,
          child: Title(
              color: Colors.white,
              child: TextButton(
                  onPressed: () {
                    loginRequest(context);
                  },
                  child: Text("立即登录",
                      style: LLAppTheme.normalTextStyle
                          .copyWith(color: Colors.white)))),
        ),
        SizedBox(
          height: 10.px,
        ),
        LLCenterWidget(
          height: 42.px,
          horizontal: 19.px,
          child: Title(
              color: Colors.white,
              child: TextButton(
                onPressed: () {},
                child: Text("忘记密码?",
                    style: LLAppTheme.smallTextStyle
                        .copyWith(color: Color.fromARGB(255, 153, 153, 153))),
              )),
        ),
        SizedBox(
          height: 100.5.px,
        ),
        Row(mainAxisAlignment: MainAxisAlignment.center, children: [
          Text.rich(TextSpan(
            children: [
              WidgetSpan(
                  child: Text(
                "登录即代表您同意",
                style: textStyle,
              )),
              WidgetSpan(
                  child: Text(
                "<路叮叮用户隐私协议>",
                style: textStyle.copyWith(
                    color: const Color.fromARGB(255, 51, 51, 51)),
              )),
              WidgetSpan(
                child: Text("和", style: textStyle),
              ),
              WidgetSpan(
                  child: Text(
                "<隐私协议>",
                style: textStyle.copyWith(
                    color: const Color.fromARGB(255, 51, 51, 51)),
              )),
            ],
          )),
        ]),
        SizedBox(
          height: 20.px,
        )
      ],
    );
  }

  // 登录
  void loginRequest(BuildContext context) {
    _phoneFocusNode.unfocus();
    _passwordFocusNode.unfocus();
    LLLoginRequest.loginRequest(_phoneController.text, _passwordController.text,
        wrapper: InterceptorsWrapper(
          onRequest: (r, h) {
            ProgressHud.showLoading(text: "正在登陆");
            h.next(r);
          },
          onResponse: (r, h) {
            print("response.data: ${json.encode(r.data)}");
            if (r.requestOptions.path == HttpConfig.userInfoUrl) {
              if (r.data["success"] == false) {
                // 登陆失败
                ProgressHud.showErrorAndDismiss(text: "登陆失败");
              } else {
                // 登陆成功
                ProgressHud.showSuccessAndDismiss(text: "登陆成功");
                Navigator.of(context).pushNamed(LLMainScreen.routeName);
              }
            } else {
              if (r.data["success"] == false) {
                // 登陆失败
                ProgressHud.showErrorAndDismiss(text: "登陆失败");
              }
            }
            h.resolve(r);
          },
          onError: (e, h) {
            ProgressHud.showErrorAndDismiss(text: "登陆失败");
            h.next(e);
          },
        ));
  }
}
